import { reqUserLogin, loginUserOut } from '@/api'

const state = {
    userInfo: {
        name:localStorage.getItem('userName'),
        token: localStorage.getItem('token'),
    }
}

const actions = {
    //登录
    async getUserLogin({commit}, obj) {
        const result = await reqUserLogin(obj)
        if(result.code === 200) {
            commit('SAVE_USER_INFO',result.data)}
        // }else {
        //     console.log(result.message)
        // }
        return result
    },
    // 登出
    async loginOut({commit}, obj) {
        const result = await loginUserOut()
        if(result.code === 200) {
            commit('LOGIN_USER_OUT')
        } else {
            console.log('登出失败');
        }
    }
}

const mutations = {
    SAVE_USER_INFO(state,payload) {
        state.userInfo.name = payload.nickName
        state.userInfo.token = payload.token

        localStorage.setItem('userName',payload.nickName)
        localStorage.setItem('token',payload.token)
    },
    LOGIN_USER_OUT(state) {
        state.userInfo.name = ''
        state.userInfo.token = ''

        localStorage.removeItem('userName')
        localStorage.removeItem('token')
    }
}


export default {
    namespaced: true,
    state,
    actions,
    mutations
}